package com.xzy.urp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.urp.common.exception.Assert;
import com.xzy.urp.common.result.ResponseEnum;
import com.xzy.urp.listener.TeacherExcelListener;
import com.xzy.urp.listener.UserExcelListener;
import com.xzy.urp.pojo.dto.ExcelTeacherDTO;
import com.xzy.urp.pojo.dto.ExcelUserDTO;
import com.xzy.urp.pojo.entity.CourseInfo;
import com.xzy.urp.pojo.entity.CourseTime;
import com.xzy.urp.pojo.entity.TeacherInfo;
import com.xzy.urp.mapper.TeacherInfoMapper;
import com.xzy.urp.pojo.entity.UserInfo;
import com.xzy.urp.pojo.vo.CourseInfoVO;
import com.xzy.urp.pojo.vo.CourseTimeVO;
import com.xzy.urp.pojo.vo.TeacherInfoVO;
import com.xzy.urp.service.CourseInfoService;
import com.xzy.urp.service.CourseTimeService;
import com.xzy.urp.service.TeacherInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzy.urp.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-02-17
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo> implements TeacherInfoService {

    private final UserInfoService userInfoService;
    private final CourseInfoService courseInfoService;
    private final CourseTimeService courseTimeService;

    @Autowired
    public TeacherInfoServiceImpl(UserInfoService userInfoService,
                                  CourseInfoService courseInfoService,
                                  CourseTimeService courseTimeService) {
        this.userInfoService = userInfoService;
        this.courseInfoService = courseInfoService;
        this.courseTimeService = courseTimeService;
    }



    @Override
    public boolean addTInfo(TeacherInfoVO teacherInfoVO) {
        //查询用户表是否有此用户
        UserInfo userInfo = userInfoService.getById(teacherInfoVO.getUId());
        TeacherInfo teacherInfo = new TeacherInfo();
        if(userInfo!=null){
            //通过后插入
            BeanUtil.copyProperties(teacherInfoVO,teacherInfo);
            baseMapper.insert(teacherInfo);

            return true;
        }



        return false;

    }

    @Override
    public TeacherInfo getInfoById(Long id) {
        //直接获取

        return baseMapper.selectById(id);
    }

    @Override
    public List<CourseInfo> getCInfoById(Long id,Integer year) {
        //直接获取
        QueryWrapper<CourseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id).eq("semester",year);
        return courseInfoService.getBaseMapper().selectList(wrapper);
    }

    @Override
    public List<CourseTimeVO> getCTimeById(Long id, Integer year) {
        //直接获取
        return courseTimeService.getByTeacherId(id,year);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelTeacherDTO.class,new TeacherExcelListener(this)).sheet().doRead();
    }

    @Override
    public void addCInfo(CourseInfoVO courseInfoVO) {
        CourseInfo courseInfo = new CourseInfo();
        BeanUtil.copyProperties(courseInfoVO,courseInfo);
        courseInfoService.save(courseInfo);

    }

    @Override
    public void updateCInfo(CourseInfoVO courseInfoVO) {
        CourseInfo courseInfo = new CourseInfo();
        BeanUtil.copyProperties(courseInfoVO,courseInfo);
        courseInfoService.updateById(courseInfo);

    }

    @Override
    public void removeCInfo(Long Cid) {
        courseInfoService.removeById(Cid);
    }
}
